package com.x.reader.dao;

import com.x.framework.Base;
import com.x.framework.dao.BaseDao;
import com.x.reader.model.Video;
import com.x.reader.model.VideoChapter;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class VideoDao extends BaseDao {

    private final static String selectVideoListSql = "select t1.* from reader_video t1, reader_category t2 where t1.category_id = t2.category_id ";

    public List<Video> selectVideoList(Video video) throws Exception {
        StringBuilder sb = new StringBuilder(selectVideoListSql);
        List params = new ArrayList();
        if (Base.isNotNull(video.getVideoId())) {
            sb.append("and t1.VIDEO_ID = ? ");
            params.add(video.getVideoId());
        }
        if (Base.isNotNull(video.getCategoryId())) {
            sb.append("and t1.CATEGORY_ID = ? ");
            params.add(video.getCategoryId());
        }
        if (Base.isNotNull(video.getVideoTitle())) {
            sb.append("and (t1.video_title like ? or t1.video_author like ?) ");
            params.add("%" + video.getVideoTitle() + "%");
            params.add("%" + video.getVideoTitle() + "%");
        }
        sb.append("order by t1.video_create_time desc");
        return this.queryForList(sb.toString(), Video.class, params, video);
    }

    private final static String selectVideoNumSql = "select count(1) as num from reader_video t1, reader_category t2 where t1.category_id = t2.category_id ";

    public int selectVideoNum(Video video) throws Exception {
        StringBuilder sb = new StringBuilder(selectVideoNumSql);
        List params = new ArrayList();
        if (Base.isNotNull(video.getVideoId())) {
            sb.append("and t1.VIDEO_ID = ? ");
            params.add(video.getVideoId());
        }
        if (Base.isNotNull(video.getCategoryId())) {
            sb.append("and t1.CATEGORY_ID = ? ");
            params.add(video.getCategoryId());
        }
        if (Base.isNotNull(video.getVideoTitle())) {
            sb.append("and (t1.video_title like ? or t1.video_author like ?) ");
            params.add("%" + video.getVideoTitle() + "%");
            params.add("%" + video.getVideoTitle() + "%");
        }
        return this.queryForInt(sb.toString(), params);
    }

    private final static String selectVideoChapterListSql = "select t1.video_chapter_id, " +
            "(t2.video_title || ' ' || t1.video_chapter_title) as video_chapter_title, " +
            "t1.video_chapter_cover, " +
            "t1.video_chapter_file, " +
            "t1.video_chapter_times, " +
            "t1.video_chapter_create_time, " +
            "t1.video_id " +
            "from reader_video_chapter t1, reader_video t2 " +
            "where t1.video_id = t2.video_id ";

    public List<VideoChapter> selectVideoChapterList(VideoChapter videoChapter) throws Exception {
        StringBuilder sb = new StringBuilder(selectVideoChapterListSql);
        List params = new ArrayList();
        if (Base.isNotNull(videoChapter.getVideoId())) {
            sb.append("and t1.video_id = ? ");
            params.add(videoChapter.getVideoId());
        }
        if (Base.isNotNull(videoChapter.getVideoChapterId())) {
            sb.append("and t1.video_chapter_id = ? ");
            params.add(videoChapter.getVideoChapterId());
        }
        sb.append("order by video_chapter_title");
        return this.queryForList(sb.toString(), VideoChapter.class, params, videoChapter);
    }

}
